const mysql = require('mysql');

module.exports = (sql,params=[]) => {
    //1.创建链接对象
    let connection = mysql.createConnection({
        host:'localhost',
        port:3306,
        user:'root',
        password:'root',
        database:'0801b_sign'
    })

    //2.链接数据库
    connection.connect((error) => {
        if(error){
            console.log("链接失败")
        }else{
            console.log("链接成功")
        }
    })

    //3.和数据库交互
    return new Promise((resolve,reject) => {
        connection.query(sql,params,(error,data) => {
            if(error){
                reject(error);
            }else{
                resolve(data);
            }
            connection.end();  //关闭链接
        })
    })
}